import { getTenantID, getUserID, requestFunCompon } from '@/utils';
import { useCallback, useMemo, useState } from 'react';
import { useRequest } from 'umi';

export const useJurisAndOperaReq = () => {
  const [selectedKeys, setSelectedKeys] = useState<string[]>([]); //树选中的key

  // stepType 为true 则是数据范围   为 false 则是功能操作
  const jurisOperaData = useRequest(
    (stepType: boolean, paramsObj: Record<string, any>) => {
      let bodyParams = {
        // positionID: getPosIDs(),
        userID: getUserID(),
        dicTypeIds: `${getTenantID()}0000`,
        pID: paramsObj?.id,
        resourceType: stepType ? '2' : '3',
      };
      let reqUrl = `/roleController/getResource`;
      return requestFunCompon('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess(data: any, params: any): void {},
    },
  );

  /* 点击菜单调用信息切换 */
  const onMenuSelectChange = useCallback(
    (menuType: boolean, menuObj: Record<string, any>) => {
      if (menuObj?.id) {
        setSelectedKeys([menuObj?.id]);
        jurisOperaData.run(menuType, menuObj);
      }
    },
    [selectedKeys],
  );

  /* 列表数据 */
  const jurisTableData = useMemo(() => {
    return {
      dataSource: jurisOperaData.data?.obj,
      tableLoading: jurisOperaData.loading,
    };
  }, [jurisOperaData.data, jurisOperaData.loading]);

  return {
    onMenuSelectChange,
    selectedKeys,
    jurisTableData,
    ...jurisOperaData,
  };
};
